package com.example.smartagriculture.controller;

import blockchain.net.NetServerScanner;
import com.example.smartagriculture.controller.DTO.LoginDTO;
import com.example.smartagriculture.controller.DTO.RePwdDTO;
import com.example.smartagriculture.service.IAccountService;
import com.example.smartagriculture.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


@RestController
@RequestMapping("/account")
public class AccountController extends Thread {
    @Resource
    private IAccountService iAccountService;
    @Operation(description = "用户登录")
    @PostMapping("/login")
    public Result save(@RequestBody LoginDTO loginDTO) {

        if (iAccountService.Login(loginDTO) != null) {
            return Result.success(iAccountService.Login(loginDTO));
        } else {
            return Result.error("账号不存在");
        }
    }

    @Operation(description = "登录获取验证码")
    @GetMapping("/verificationCode")
    public Result getCode() {
        return Result.success(iAccountService.getCode());
    }

    @Operation(description = "重置密码验证码")
    @PostMapping("/mailCode")
    public Result mailRepwd(@RequestBody RePwdDTO rePwdDTO) {
        if (iAccountService.mailCode(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    @Operation(description = "重置密码")
    @PostMapping("/rePwd")
    public Result RePwd(@RequestBody RePwdDTO rePwdDTO) {
        if (iAccountService.rePwd(rePwdDTO)) {
            return Result.success(iAccountService.rePwd(rePwdDTO));
        }
        return Result.error("重置密码失败");
    }

    @Operation(description = "校验")
    @PostMapping("/verifyCode")
    public Result verifyCode(@RequestBody RePwdDTO rePwdDTO) {
        if (iAccountService.verifyCode(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    @Operation(description = "注册")
    @PostMapping("/signup")
    public Result signup(@RequestBody RePwdDTO rePwdDTO) {
        if (iAccountService.signup(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }
    @Operation(description = "查找所有")
    @GetMapping("/selectAll")
    public Result selectAll(){
        return Result.success(iAccountService.selectAll());
    }
}
